﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class morning_EditGdzgTop : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            bd();
        }
    }

    void bd()
    {
        string id = Request["id"];
        int Id = int.Parse(id);
        TemporaryModel.TemporaryEntities dal = DAL.DBManager.GetTempEmtity();
        TemporaryModel.StockBusinessTop sbt = dal.StockBusinessTop.First(x => x.id == Id);
        lblid.Text = sbt.id.ToString();
        txtUserName.Text = sbt.userName;
        txtyDayTop.Text = sbt.ydayTop.ToString();
        txtDayRate.Text = sbt.dayrate.ToString();
        txtTotalRate.Text = sbt.totalrate.ToString();
        txtStockPositions.Text = sbt.stockPositions.ToString();
        txtdProfitRate.Text = sbt.dProfitRate.ToString();
        txtAmount.Text = sbt.amount.ToString();
        txtTotalAmount.Text = sbt.totalAmount.ToString();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        TemporaryModel.TemporaryEntities dal = DAL.DBManager.GetTempEmtity();
        int id = int.Parse(lblid.Text);
        TemporaryModel.StockBusinessTop sbt = dal.StockBusinessTop.First(x => x.id == id);
        try
        {
            sbt.userName = txtUserName.Text;
            sbt.ydayTop = int.Parse(txtyDayTop.Text);

            sbt.dayrate = GetNum(txtDayRate.Text);
            sbt.totalrate = GetNum(txtTotalRate.Text);
            sbt.stockPositions = GetNum(txtStockPositions.Text);
            if (txtdProfitRate.Text != null && txtdProfitRate.Text != "")
                sbt.dProfitRate = GetNum(txtdProfitRate.Text);

            sbt.amount = decimal.Parse(txtAmount.Text);

            sbt.totalAmount = decimal.Parse(txtTotalAmount.Text);
            sbt.wtDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
            try
            {
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "save", "alert('修改成功'),document.location='gdzgTop.aspx'", true);
               
            }
            catch(Exception ex)
            {
                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "editStockBusinessTop", LogYuMin.LogStates.ERROR, ex);
                ClientScript.RegisterStartupScript(GetType(), "save", "alert('修改失败')", true);
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "save", "alert('录入数据格式有误！请检查数据格式是否正确')", true);
        }
    }
    public decimal GetNum(string str)
    {
        if (str.Contains("%"))
        {
            str = str.Replace("%", "");
            decimal dl = decimal.Parse(str) / 100;
            return dl;
        }
        else
        {
            return decimal.Parse(str);
        }
    }
}